﻿var editedRowId;
var fixedStatus = ['ACCEPTED', 'FACTORY', 'TRANSPORT', 'ARRIVIED'];

function tdToForm(event) {

    event.stopPropagation();

    var parentRow = $(this).parent();
    var data = table.row(this).data();

    var newEditedRowId = data[0];

    console.log(editedRowId);
    console.log(newEditedRowId);

    if ((typeof editedRowId == "undefined" || (typeof editedRowId != "undefined" && newEditedRowId != editedRowId))
        && (parentRow.children()).toArray().indexOf(this) == 5) {

        //$('#ordertable tbody').off('click', 'td', tdToForm);

        if (typeof editedRowId != "undefined") {
            var selectedList = $("#tablestatus");

            var parentTd = selectedList.parent();

            var tempdata = table.row(parentTd).data();

            selectedList.remove();
            parentTd.html(tempdata[5]);


        }


        editedRowId = newEditedRowId;

        //console.log('You clicked on ' + data[5] + '\'s row');

        var status = data[5];

        var statusList = $("<select></select>");
        statusList.attr('id', 'tablestatus');


        var statusIndex = fixedStatus.indexOf(status);

        option = $("<option></option>");
        option.attr("value", "0");
        option.html("ACCEPTED");
        statusList.append(option);

        option = $("<option></option>");
        option.attr("value", "1");
        option.html("FACTORY");
        statusList.append(option);

        option = $("<option></option>");
        option.attr("value", "2");
        option.html("TRANSPORT");
        statusList.append(option);

        option = $("<option></option>");
        option.attr("value", "3");
        option.html("ARRIVIED");
        statusList.append(option);

        $(this).html("");
        $(this).append(statusList);

        $('#tablestatus option[value=' + statusIndex + ']').prop("selected", true);

        $('#tablestatus').change(function () {
            var selectedIndex = $('#tablestatus').val();
            var newStatus = fixedStatus[selectedIndex];

            var parentTd = $(this).parent();

            var tempdata = table.row(parentTd).data();

            var list = $(this);

            $.post("setOrderState", { id: editedRowId, "newState": newStatus },
              function succ(response) {
                  list.remove();

                  data[5] = newStatus;
                  table.row(parentTd).data(data).draw();

                  editedRowId = undefined;

              }).fail(
                function () {
                    list.remove();

                    //data[5] = newStatus;
                    table.row(parentTd).data(data).draw();

                    editedRowId = undefined;
                }


              );







        });

    }
}


function refreshImage() {

    $("#factorylistimage").html("");

    var selectedFactoryName = $("#factory option:selected").html();

    if (typeof listImages[selectedFactoryName] != "undefined") {
        var newImage = $("<img/>");
        newImage.attr("src", listImages[selectedFactoryName]);

        $("#factorylistimage").append(newImage);
    }
}

function reloadTable() {

    var orderTable = $("#tablecontent");

    //orderTable.html("");

    var selectedFactoryId = $("#factory").val();

    $.post("getOrders", { factoryId: selectedFactoryId },
    function (data) {
        if (data.length != 0) {

            orderArray = [];

            for (var i = 0; i < data.length; i++) {

                /*var tableRow = $("<tr></tr>");
                td = $("<td></td>");

                td.html(data[i].Id);
                tableRow.append(td);

                td = $("<td></td>");
                td.html(data[i].Producer);
                tableRow.append(td);

                td = $("<td></td>");
                td.html(data[i].Product);
                tableRow.append(td);

                td = $("<td></td>");
                td.html(data[i].Date);
                tableRow.append(td);

                td = $("<td></td>");
                td.html(data[i].Quantity);
                tableRow.append(td);

                td = $("<td></td>");
                td.html(data[i].State);
                tableRow.append(td);

                orderTable.append(tableRow);*/

                orderArray.push([data[i].Id, data[i].Producer, data[i].Product, data[i].Date,
                data[i].Quantity, data[i].State])
            }


            table = $('#orders').DataTable({
                destroy: true,
                data: orderArray,
                columns: [
                    { title: "ID" },
                    { title: "Producer" },
                    { title: "Product" },
                    { title: "Date" },
                    { title: "Pieces" },
                    { title: "State" }
                ]

            });

            $('#ordertable tbody').on('click', 'td', tdToForm);

        }

    });

}

$(document).ready(function () {

    $(document).click(
        function () {
            var selectedList = $("#tablestatus");

            if (selectedList.length) {

                var table = $('#orders').DataTable();

                var parentTd = selectedList.parent();

                var tempdata = table.row(parentTd).data();

                selectedList.remove();
                parentTd.html(tempdata[5]);

                editedRowId = undefined;
            }
        }
    );

    refreshImage();
    reloadTable();

    $("#factory").change(function () {

        refreshImage();
        reloadTable();

    });

});
